<?php

namespace BWL\webshopBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use JMS\SecurityExtraBundle\Annotation\Secure;

/**
 * Created by JetBrains PhpStorm.
 * User: leon
 * Date: 11/23/12
 * Time: 11:36 PM
 * To change this template use File | Settings | File Templates.
 */
class MyAccountController extends Controller
{
  /**
   * @Secure(roles="ROLE_USER, ROLE_ADMIN")
   */
  public function myaccountAction() {
    return $this->render('BWLwebshopBundle:MyAccount:myaccount.html.twig');
  }

  /**
   * @Secure(roles="ROLE_USER, ROLE_ADMIN")
   */
  public function myorderingsAction() {
    $user = $user = $this->get('security.context')->getToken()->getUser();
    $doctrine = $this->getDoctrine();
    $orderings = $doctrine->getRepository('BWLwebshopBundle:Ordering')->findBy(array('user' => $user), array('orderDate' => 'ASC'));
    $totalPrice = array();
    $orderProps = array();
    foreach($orderings as $orderEntry) {
      if(!array_key_exists($orderEntry->getId(), $totalPrice)){
        $totalPrice[$orderEntry->getId()] = $orderEntry->getPrice() * $orderEntry->getCount();
    }else{
        $totalPrice[$orderEntry->getId()] += $orderEntry->getPrice() * $orderEntry->getCount();
      }
      $orderProps[$orderEntry->getId()] = array(
        'orderState' => $orderEntry->getState()->getName(),
        'totalPrice' => $totalPrice[$orderEntry->getId()],
        'orderDate' => $orderEntry->getOrderDate()->format('Y-m-d H:i:s')
      );

    }

    return $this->render('BWLwebshopBundle:MyAccount:myorderings.html.twig', array('orderProps' => $orderProps));

  }

}
